package com.intel.two.controller.smarthome;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.intel.two.bo.smarthome.HouseholdBo;
import com.intel.two.common.R;
import com.intel.two.exception.CustomerException;
import com.intel.two.listener.smarthome.WebHouseholdListener;
import com.intel.two.pojo.smarthome.Household;
import com.intel.two.service.smarthome.HouseholdService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.building.BarVo;
import com.intel.two.vo.smarthome.HouseholdVo;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.aspectj.util.FileUtil;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: intelligentcommunity
 * @BelongsPackage: com.intel.two.controller.smarthome
 * @Author: yanhongwei
 * @CreateTime: 2022-10-13  22:45
 * @Description: TODO
 * @Version: 1.0
 */

@RestController
@RequestMapping("/household")
public class HouseholdController {

    @Autowired
    private HouseholdService householdService;

    @GetMapping("/householdList")
    public R householdList(@RequestParam Map<String, Object> parameters) {
        return householdService.householdList(parameters);
    }
    @GetMapping("/householdList1")
    public R householdList1(@RequestParam Map<String, Object> parameters) {
        return householdService.householdList1(parameters);
    }

    //添加时文件上传
    @RequestMapping("/householdUpload")
    public R householdUpload(MultipartFile file) throws Exception {
//        文件名字
        String filename = file.getOriginalFilename();
//
        InputStream filepath = file.getInputStream();
        assert filename != null;
        String fileUrl = OssUtil.updateLoad(filepath, filename);

        return R.ok().data("fileUrl", fileUrl);
    }

    //    /household/householdAdd
    @RequestMapping("/householdAdd")
    public R householdAdd(@RequestBody Household household)throws CustomerException {
        return householdService.householdAdd(household);
    }

    @PostMapping("/householdEdit")
    public R householdEdit(@RequestBody Household household)throws CustomerException {

        return householdService.householdEdit(household);
    }

    // 删除多行
    @PostMapping("/househomesDel")
    public R househomesDel(@RequestBody List<Household> householdList)throws CustomerException {

        return householdService.househomesDel(householdList);
    }

    //删除，删除一行
    @PostMapping("/househomeDel")
    public R househomeDel(@RequestBody Household household)throws CustomerException {

        return householdService.househomeDel(household);
    }

    //折线图
    @GetMapping("/echarts1")
    public R echarts1()throws CustomerException {
        return householdService.echarts1();
    }
    @Autowired
    private WebHouseholdListener webHouseholdListener;

    @RequestMapping("/import")
    public R householdImport1(MultipartFile file)throws CustomerException, IOException {

        /**
         * 参数1  文件流
         * 参数2 读取到的数据封装到哪个类型
         * 参数3 监听器  每读取一行数据 都会调用监听器的 invoke 方法
         *
         * */
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), HouseholdBo.class, webHouseholdListener);
        //得到 读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();

        return R.ok();
    }



    @RequestMapping("/export")
    public void export(HouseholdVo household, HttpServletResponse response)throws CustomerException, IOException {
        //文件的下载   关键点 要设置 响应头

        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("家具信息导出", "utf-8");

        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");


        // 得到 输出流 输出流向浏览器输出数据
        ServletOutputStream outputStream = response.getOutputStream();

        // easyexcel 写数据到 输出流
        ExcelWriterBuilder write = EasyExcel.write(outputStream, HouseholdBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        List<HouseholdBo> households = householdService.householdExcelData(household);

//        写到表格
        sheet.doWrite(households);
    }

    @RequestMapping("/escelModel")
    public void escelModel(HttpServletRequest request, HttpServletResponse response) throws IOException, CustomerException {
        //文件的下载   关键点 要设置 响应头

        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
//
        String realPath = request.getServletContext().getRealPath("/excel/家居清单.xlsx");


        String realPath1 = request.getContextPath();
        System.out.println(realPath);

        File file = new File(realPath);
        byte[] bytes = FileUtil.readAsByteArray(file);
        //防止中文乱码
        String filename = URLEncoder.encode("家居清单", "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        // 得到 输出流 输出流向浏览器输出数据
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.close();
    }



}


